#include "CTranslateWidget.h"
#include "../Common/CCommonFunction.h"
#include "ui_CTranslateWidget.h"

#include <QMessageBox>

const QString STR_TITLE = "翻译生成";

const QString FILE_NAME = "Translate.xml";
const QString MODEL_NAME = "Translate";

QString CTranslateWidget::s_strFileName = QString();

CTranslateWidget::CTranslateWidget(QWidget* parent)
    : CMyWindow(parent),
      ui(new Ui::CTranslateWidget)
{
    ui->setupUi(this);

    CCommonFunction::GetDataSavePath(s_strFileName);
    s_strFileName += FILE_NAME;

    this->setWindowTitle(STR_TITLE);

    m_pTranslateResultWidget = new CTranslateResultWidget;
}

CTranslateWidget::~CTranslateWidget()
{
    delete ui;

    if (m_pTranslateResultWidget) {
        delete m_pTranslateResultWidget;
        m_pTranslateResultWidget = nullptr;
    }
}

void CTranslateWidget::LoadSize(QWidget* parent)
{
    CMyWindow::LoadSizeFromFile(parent, s_strFileName);
}

void CTranslateWidget::SaveSize(QWidget* parent)
{
    CMyWindow::SaveSizeToFile(parent, s_strFileName, MODEL_NAME);
}

QMenuBar* CTranslateWidget::GetMenuBar()
{
    return nullptr;
}

int CTranslateWidget::GetComponentId()
{
    return 2;
}

void CTranslateWidget::CreateTranslateResult()
{
    // 提取变量名
    QStringList listVariable = ui->pteVariable->toPlainText().split("\n");
    QStringList listTranslate = ui->pteResource->toPlainText().split("\n");
    listTranslate.removeAll("");

    // 判断是否保留换行
    if (ui->cbKeepLine->checkState() != Qt::Checked) {
        listVariable.removeAll("");
    }

    // 获取翻译内容
    if (listTranslate.isEmpty()) {
        m_pTranslateResultWidget->SetItemCount(0);
    }
    else {
        int count = listTranslate.at(0).split("\t").size();
        m_pTranslateResultWidget->SetItemCount(count);
    }

    // 拼接结果
    int resultIndex = 0;
    int resultCount = 0;  // 拼接的数量
    for (int i = 0; i < listVariable.size(); i++) {
        QString text = listVariable.at(i);
        if (!text.isEmpty()) {
            // 提取变量名
            int equalIndex = text.indexOf("=");   // 等号索引
            int ratherIndex = text.indexOf(">");  // xml标记第一个出现的 > 的索引
            if (equalIndex != -1) {
                text = text.mid(0, equalIndex);
            }

            if (ratherIndex != -1) {
                text = text.mid(1, ratherIndex - 1);
            }
        }

        // 拼接结果
        if (resultIndex >= listTranslate.size()) {
            break;
        }

        QStringList listResource = listTranslate.at(resultIndex).split("\t");
        for (int j = 0; j < listResource.size(); j++) {
            if (!text.isEmpty()) {
                QString result;
                if (ui->rbXMLFormat->isChecked()) {
                    result = QString("<%1>%2</%1>").arg(text).arg(listResource.at(j));
                }
                else if (ui->rbEquialFormat->isChecked()) {
                    result = QString("%1=%2").arg(text).arg(listResource.at(j));
                }
                m_pTranslateResultWidget->AppendItemText(j, result + "\n");
                ++resultCount;
            }
            else {
                m_pTranslateResultWidget->AppendItemText(j, "\n");
            }
        }
        if (!text.isEmpty()) {
            ++resultIndex;
        }
    }

    // 数量不正确提示
    if (resultIndex < listTranslate.size()) {
        QMessageBox::information(this, "提示", "请注意，变量数小于资源数！");
    }

    if (resultCount < listTranslate.size()) {
        QMessageBox::information(this, "提示", "请注意，变量数大于资源数！");
    }
}

void CTranslateWidget::on_btnCreate_clicked()
{
    CreateTranslateResult();
    m_pTranslateResultWidget->show();
}
